package com.huike.clues.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.dto.TbActivityDTO;
import com.huike.clues.service.TbActivityService;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.common.utils.uuid.UUIDUtils;
import net.sf.jsqlparser.statement.select.Offset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 93238
* @description 针对表【tb_activity(活动管理)】的数据库操作Service实现
* @createDate 2023-10-12 06:35:46
*/
@Service
public class TbActivityServiceImpl extends ServiceImpl<TbActivityMapper, TbActivity> implements TbActivityService{

    @Autowired
    private TbActivityMapper tbActivityMapper;


    /**
     * 获取活动列表
     * @param dto 查询参数
     * @return 活动列表
     */
    @Override
    public List<TbActivity> getList(TbActivityDTO dto) {
        return tbActivityMapper.getList(dto);
    }

    /**
     * 新增活动
     * @param dto 新增参数
     */
    @Override
    @Transactional
    public void add(TbActivityDTO dto) {
        String name = dto.getName();
        List<TbActivity> list = this.list(Wrappers.<TbActivity>lambdaQuery().eq(TbActivity::getName, name));
        if (CollectionUtil.isNotEmpty( list)){
            throw new RuntimeException("活动名称已存在");
        }
        TbActivity activity = BeanUtil.toBean(dto, TbActivity.class);
        activity.setCode(UUIDUtils.getUUID());
        this.save(activity);
    }

    /**
     * 修改活动
     *
     * @param dto 修改参数
     */
    @Override
    @Transactional
    public void updateActivity(TbActivityDTO dto) {
        String name = dto.getName();
        List<TbActivity> list = this.list(Wrappers.<TbActivity>lambdaQuery().eq(TbActivity::getName, name));

        if (CollectionUtil.isNotEmpty(list) ){
            list.forEach(x -> {
                if (dto.getId() - x.getId() != 0){
                    throw new RuntimeException("活动名称已存在");
                }
            });
        }

        TbActivity activity = BeanUtil.toBean(dto, TbActivity.class);
        this.updateById(activity);
    }

    /**
     * 获取状态为为2的渠道活动列表
     * @param channel
     */
    @Override
    public List<TbActivity> getByChannel(String channel) {
        List<TbActivity> list = this.lambdaQuery()
                .eq(TbActivity::getChannel, channel)
                .list();
        return list;
    }

    /**
     * 删除活动
     *
     * @param ids
     */
    @Override
    public void deleteByIds(String ids) {
        List<Long> idList = StrUtil.split(ids, ",")
                .stream()
                .map(x -> Long.valueOf(x))
                .collect(Collectors.toList());
        this.removeByIds(idList);
    }
}




